#!/bin/bash

function get_runtime_dts() {
	if [[ "$1" == "-" ]]; then
		rm fdt
		rm fdt.dts

		echo "Success: clean"
		return 0
	fi

	device="$1"

	if [ -e fdt ]; then
		echo "Existing: fdt"
		return 1
	fi

	if [ -e fdt.dts ]; then
		echo "Existing: fdt.dts"
		return 1
	fi

	if [[ "$device" == "" ]]; then
		adb shell "su root cat /sys/firmware/fdt" > fdt
	else
		adb -s "$device" shell "su root cat /sys/firmware/fdt" > fdt
	fi
	if [[ "$?" != "0" ]]; then
		echo 'adb shell "su root cat /sys/firmware/fdt" > fdt'
		rm fdt
		return $?
	fi

	dtc -Idtb -Odts fdt > fdt.dts 2>/dev/null

	echo "Success: fdt.dts"
}
